package com.dream.system.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.dream.common.Common;
import com.dream.common.DataInit;
import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.QQImage;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_File;
import com.dream.system.service.ISysUser;

@Controller
@RequestMapping("/edu/sysUserAction.do")
@SuppressWarnings("all")
public class SysUserAction extends SuperController {

	@Autowired
	private ISysUser sysUserImpl;

	/**
	 * 增加用户
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addUser")
	public void addUser(@RequestParam("File1") MultipartFile f, SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		if (null != f && null != f.getOriginalFilename()) {
			try {
				String str1 = f.getOriginalFilename();
				byte[] b = f.getBytes();
				String str = QQImage.QQupload(b);
				model.setUserImage(str);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		//以下是循环增加，测试代码
//		int temp =0;
//		long tell=Long.parseLong(model.getTell());
//		for (int i = 0; i < 100; i++) { 
//			SysUser su  = new SysUser();
//			BeanUtils.copyProperties(model, su);
//			su.setTell((tell+i)+"");
//			su.setUserName(model.getUserName()+i);
//			su.setUserShowName(model.getUserShowName()+i);
//			su.setUserEngListName(model.getUserEngListName()+i);
//			temp= this.sysUserImpl.addUser(su);
//		}
		int temp =this.sysUserImpl.addUser(model);
		
		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else if (temp == 2) {
			Msg msg = this.getMsg("0", "已经存在相同名称或者电话号码，请更换电话号码或者账号名称在进行操作!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();

		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询用户
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=get64User")
	public void get64User(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysUserImpl.getAllUser("6", "4", model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 删除用户
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteUser")
	public void deleteUser(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int temp = this.sysUserImpl.deleteUser(request.getParameter("allId"));
		if (temp > 0) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 删除用户
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=disUser")
	public void disUser(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String allId = request.getParameter("allId");
		String flag = request.getParameter("flag");
		int temp = this.sysUserImpl.deleteDisUser(allId, flag);
		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询老师和专家
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=get25User")
	public void get25User(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysUserImpl.getAllUser("5", "2", model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询老师和专家
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=get1User")
	public void get1User(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysUserImpl.getAllUser("1", "0", model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询这个学校，这个班的老师
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getClassTeacher")
	public void getClassTeacher(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysUserImpl.getClassTeacher(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询这个学校，这个班的学生
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getClassStudent")
	public void getClassStudent(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysUserImpl.getClassStudent(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 这里的iNfo，只查询学生所有老师的列表就已经足够了
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudentInfo")
	public void getStudentInfo(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sysUserImpl.getStudentInfo(model, "");
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 这里的iNfo,查询他的基本信息
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudentBaicInfo")
	public void getStudentBaicInfo(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser map = this.sysUserImpl.getStudentBaicInfo(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 这里只是批的老师查询自己所有教过的班
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacherClass")
	public void getTeacherClass(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysUserImpl.getTeacherClass(sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 这里只是学生所有上过的班
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudentClass")
	public void getStudentClass(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysUserImpl.getStudentClass(sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询学生当前在教的老师信息
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudent_Teacher_C")
	public void getStudent_Teacher_C(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			System.out.println("操作失败!没有找到对应的学生");
			Msg msg = this.getMsg("0", "操作失败!没有找到对应的学生", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysUserImpl.getStudentInfo(sysUser, "1");
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询一个班当前的学生
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacherClassStudent")
	public void getTeacherClassStudent(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String jie = request.getParameter("year");

		if (null == jie || "".equals(jie)) {
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if (null == model.getCid() || "".equals(model.getCid())) {
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		Map map = this.sysUserImpl.getTeacherClassStudent(model, jie);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

}
